package com.ruitu.mall_98_seller.mvp.presenter;

import android.text.TextUtils;

import com.google.zxing.integration.android.IntentIntegrator;
import com.ruitu.arad.api.BaseResult;
import com.ruitu.arad.api.PageModel;
import com.ruitu.arad.base.BasePresenter;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.dao.bean.AfterSale;
import com.ruitu.mall_98_seller.dao.bean.Express;
import com.ruitu.mall_98_seller.dao.bean.Order;
import com.ruitu.mall_98_seller.dao.bean.OrderFilter;
import com.ruitu.mall_98_seller.dao.bean.OrderInfo;
import com.ruitu.mall_98_seller.mvp.model.OrderModel;
import com.ruitu.mall_98_seller.support.zxing.QrCodeActivity;
import com.ruitu.mall_98_seller.ui.order.OrderDetail2Activity;
import com.ruitu.mall_98_seller.ui.order.OrderListActivity;

import java.text.SimpleDateFormat;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class OrderPresenter {

    public static final String TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    // 订单列表
    public static class OrderListP extends BasePresenter<OrderListActivity, OrderModel.OrderListM> {

        public Disposable reqOrderList(String comId, int size, int page, String status, String type) {
            return m.reqOrderList(comId, size, page, status, type)
                    .subscribe(new Consumer<BaseResult<PageModel<Order>>>() {
                        @Override
                        public void accept(BaseResult<PageModel<Order>> pageResult) throws Exception {
                            v.hideProgress();
                            if (pageResult.isSuccess()) {
                                v.onReqComplete(1, true, pageResult.getData());
                            } else {
                                ToastUtils.showShortSafe(pageResult.getMsg());
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            v.hideProgress();
                            ToastUtils.showShortSafe("获取订单异常,请稍候重试");
                        }
                    });
        }

        public Disposable sendMsg(String oId, String type) {
            return m.sendMsg(oId, type).subscribe(new Consumer<BaseResult>() {
                @Override
                public void accept(BaseResult result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(2, true, result.getMsg());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    ToastUtils.showShortSafe("异常,请稍后重试");
                    throwable.printStackTrace();
                }
            });
        }

        // 获取快递公司列表
        public Disposable reqLogList() {
            return m.reqLogList().subscribe(new Consumer<BaseResult<List<Express>>>() {
                @Override
                public void accept(BaseResult<List<Express>> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(3, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("获取快递公司异常,请重试");
                }
            });
        }

        // 发货  小订单id 快递公司code 快递单号
        public Disposable reqSendGoods(String oId, String logCode, String logNum) {
            return m.reqSendGoods(oId, logCode, logNum).subscribe(new Consumer<BaseResult>() {
                @Override
                public void accept(BaseResult result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(4, true, result.getMsg());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("发货异常,请稍候重试");
                }
            });
        }

        // 获取售后详情
        public Disposable reqAfterSale(String oId) {
            return m.reqAfterSale(oId).subscribe(new Consumer<BaseResult<AfterSale>>() {
                @Override
                public void accept(BaseResult<AfterSale> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(5, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("请求异常,请稍后重试");
                }
            });
        }

        // 卖家确认退款
        public Disposable reqRefund(String oId, String status) {
            return m.reqRefund(oId, status).subscribe(new Consumer<BaseResult>() {
                @Override
                public void accept(BaseResult result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(6, true, result.getMsg());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    throwable.printStackTrace();
                    ToastUtils.showShortSafe("审批异常,请稍候重试");
                }
            });
        }

        // 开启扫码功能 activity重写onActivityResult()方法接收数据
        public void startScanCode() {
            new IntentIntegrator(activity)
                    .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选：一维码，二维码，一/二维码
//                    .setPrompt("请对准二维码")// 设置提示语
                    .setCameraId(0)// 选择摄像头,可使用前置或者后置
                    .setBeepEnabled(true)// 是否开启声音,扫完码之后会"哔"的一声
                    .setCaptureActivity(QrCodeActivity.class)//自定义扫码界面
                    .initiateScan();// 初始化扫码
        }

        // 生成订单筛选条件数据
        public void genOrderFilter(List<OrderFilter> filterList1, List<OrderFilter> filterList2) {
            // 状态筛选的数据
            filterList1.add(new OrderFilter("全部状态", ""));
            filterList1.add(new OrderFilter("待支付", "0"));
            filterList1.add(new OrderFilter("已取消", "1"));
            filterList1.add(new OrderFilter("已支付", "2"));
            filterList1.add(new OrderFilter("待收货", "4"));
            filterList1.add(new OrderFilter("待评价", "5"));
            filterList1.add(new OrderFilter("售后", "6"));
            filterList1.add(new OrderFilter("已完成", "7"));
            filterList1.add(new OrderFilter("已退款", "8"));
            // 类型筛选的数据
            filterList2.add(new OrderFilter("全部类型", ""));
            filterList2.add(new OrderFilter("直销单", "0"));
            filterList2.add(new OrderFilter("预售单", "1"));
            filterList2.add(new OrderFilter("预订单", "2"));
            filterList2.add(new OrderFilter("拼团单", "3"));
        }

        public int getColorByType(String typeCode) {
            if (TextUtils.equals("0", typeCode)) {
                return R.color.text_blue;
            } else if (TextUtils.equals("1", typeCode)) {
                return R.color.text_black;
            } else if (TextUtils.equals("2", typeCode)) {
                return R.color.text_green;
            } else if (TextUtils.equals("3", typeCode)) {
                return R.color.text_red;
            }
            return R.color.text_black;
        }

        // 订单类型 0 直销单 1 预售单 2 预订单 3 拼单
        public String getType(String typeCode) {
            if (TextUtils.equals("0", typeCode)) {
                return "直销单";
            } else if (TextUtils.equals("1", typeCode)) {
                return "预售单";
            } else if (TextUtils.equals("2", typeCode)) {
                return "预订单";
            } else if (TextUtils.equals("3", typeCode)) {
                return "拼团单";
            }
            return "";
        }

        // 订单状态 0 未支付 1 已取消 2 已支付待发货 4 已发货待收货 5 已收货待评价 6 申请售后 7 已完成 8 退款已完成
        // 大订单支付状态 0 待支付 1 已支付 2 已支付订金 3 已取消 4 已通知用户支付尾款 5 删除
        // 大订单状态,小订单状态,拼团成功与否状态,订单类型
        // collage_status：是否拼团成功(0 未拼成 1 已拼成 2 进行中)
        public String getStatus(String statusCode, String bigStatus, String groupStatus, String type) {
            if (TextUtils.equals("0", statusCode)) {// 待支付
                // 预订或预售单
                if (TextUtils.equals("1", type) || TextUtils.equals("2", type)) {
                    if (TextUtils.equals("0", bigStatus)) {
                        return "待支付";
                    } else if (TextUtils.equals("1", bigStatus)) {

                    } else if (TextUtils.equals("2", bigStatus)) {
                        return "已支付订金";
                    } else if (TextUtils.equals("3", bigStatus)) {

                    } else if (TextUtils.equals("4", bigStatus)) {
                        return "已通知用户支付尾款";
                    } else if (TextUtils.equals("5", bigStatus)) {

                    }
                }
                return "待支付";
            } else if (TextUtils.equals("1", statusCode)) {
                return "已取消";
            } else if (TextUtils.equals("2", statusCode)) { // 已支付
                // 拼团类型 已支付需要判断是否拼团成功 y 发货 n 等待拼团成功
                if (TextUtils.equals("3", type)) {
                    if (TextUtils.equals("0", groupStatus)) {// 拼团失败
                        return "拼团失败";
                    } else if (TextUtils.equals("1", groupStatus)) {// 拼团成功
                        return "拼团成功等待发货";
                    } else if (TextUtils.equals("2", groupStatus)) {// 拼团进行中
                        return "拼团进行中";
                    }
                }
                return "待发货";
            } else if (TextUtils.equals("4", statusCode)) {
                return "待收货";
            } else if (TextUtils.equals("5", statusCode)) {
                return "待评价";
            } else if (TextUtils.equals("6", statusCode)) {
                return "售后";
            } else if (TextUtils.equals("7", statusCode)) {
                return "已完成";
            } else if (TextUtils.equals("8", statusCode)) {
                return "已退款";
            }

            return "";
        }
    }

    // 订单详情
    public static class OrderDetailP extends BasePresenter<OrderDetail2Activity, OrderModel.OrderDetailM> {
        public Disposable reqOrderDetail(String oId) {
            return m.reqOrderDetail(oId).subscribe(new Consumer<BaseResult<Order>>() {
                @Override
                public void accept(BaseResult<Order> result) throws Exception {
                    v.hideProgress();
                    if (result.isSuccess()) {
                        v.onReqComplete(1, true, result.getData());
                    } else {
                        ToastUtils.showShortSafe(result.getMsg());
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    v.hideProgress();
                    ToastUtils.showShortSafe("获取订单详情异常,请稍候重试");
                    throwable.printStackTrace();
                }
            });
        }

        public void genOrderInfoList(Order d, List<OrderInfo> l1, List<OrderInfo> l2
                , List<OrderInfo> l3, List<OrderInfo> l4, List<OrderInfo> l5, List<OrderInfo> l6) {
            if (null != d) {
                Order.OrderIn oIn = d.getOrder();// 大订单信息
                // 基本信息
                l1.add(new OrderInfo("订单编号", d.getOrderId()));
                l1.add(new OrderInfo("订单状态", new OrderListP().getStatus(d.getOrderStatus(), oIn.getIsPayOver(), oIn.getCollageStatus(), d.getOrderType())));
                l1.add(new OrderInfo("买家用户", oIn.getAccountName()));
                l1.add(new OrderInfo("联系方式", oIn.getCollectGoodsPhone()));
                l1.add(new OrderInfo("支付方式", getPayType(d.getPaymentType())));
                l1.add(new OrderInfo("订单总额", oIn.getOrderTotal() + "元"));
                l1.add(new OrderInfo("优惠金额", oIn.getDiscountTotal() + "元"));
                l1.add(new OrderInfo("订单类型", new OrderListP().getType(d.getOrderType())));
                l1.add(new OrderInfo("创建时间", new SimpleDateFormat(TIME_PATTERN).format(d.getCreateTime())));
                l1.add(new OrderInfo("支付时间", null == d.getPayTime() ? "暂未支付" : new SimpleDateFormat(TIME_PATTERN).format(d.getPayTime())));
                l1.add(new OrderInfo("发货时间", null == d.getShippingTime() ? "暂未发货" : new SimpleDateFormat(TIME_PATTERN).format(d.getShippingTime())));
                l1.add(new OrderInfo("完成时间", null == d.getFinishTime() ? "暂未完成" : new SimpleDateFormat(TIME_PATTERN).format(d.getFinishTime())));
                // 商品信息
                l2.add(new OrderInfo("商品名称", d.getGoodsName()));
                l2.add(new OrderInfo("商品数量", d.getQuantity()));
                l2.add(new OrderInfo("商品价格", d.getGoodsTotal() + "元"));
                l2.add(new OrderInfo("商品型号", TextUtils.isEmpty(d.getSkuContent()) ? "  " : d.getSkuContent()));
                l2.add(new OrderInfo("商品重量", d.getWeight() + "kg"));// 重量单位 kg
                l2.add(new OrderInfo("商品运费", d.getFreight() + "元"));
                l2.add(new OrderInfo("手  续  费", d.getCharge() + "元"));
                l2.add(new OrderInfo("销售企业", d.getCompanyName()));
                // 预售信息
                Order.PreSale pre = d.getGoodsPreSale();
                if (null != pre) {
                    l3.add(new OrderInfo("预付订金", oIn.getAdvanceMoney() + "元"));
                    l3.add(new OrderInfo("需付尾款", oIn.getTailMoney() + "元"));
                    l3.add(new OrderInfo("预付比例", pre.getPreSaleRatio() + "%"));
                    l3.add(new OrderInfo("发货时间", pre.getPreSaleDelivey() + "天"));
                }
                // 预订信息
                Order.Reserve res = d.getGoodsReserve();
                if (null != res) {
                    l4.add(new OrderInfo("预付订金", oIn.getAdvanceMoney() + "元"));
                    l4.add(new OrderInfo("需付尾款", oIn.getTailMoney() + "元"));
                    l4.add(new OrderInfo("预订状态", getReserveStatus(oIn.getIsPayOver())));
                    l4.add(new OrderInfo("发货时间", null == res.getDeliveryTime() ? "暂未发货" : new SimpleDateFormat(TIME_PATTERN).format(res.getDeliveryTime())));
                    l4.add(new OrderInfo("需求描述", res.getIntro()));
                }
                // 拼团信息
                Order.Collage col = d.getGoodsCollage();
                if (null != col) {
                    l5.add(new OrderInfo("拼团人数", col.getCollageNum()));
                    l5.add(new OrderInfo("现有人数", col.getExistingNum()));
                    l5.add(new OrderInfo("拼团状态", getCollageStatus(col.getStatus())));
                    l5.add(new OrderInfo("超时时间", null == col.getEndTime() ? "暂未超时" : new SimpleDateFormat(TIME_PATTERN).format(col.getEndTime())));
                }
                // 收货信息
                l6.add(new OrderInfo("收  货  人", oIn.getConsigneeName()));
                l6.add(new OrderInfo("联系电话", oIn.getCollectGoodsPhone()));
                l6.add(new OrderInfo("收货地区", oIn.getOrderProvinceCode() + oIn.getOrderCityCode() + oIn.getOrderCountyCode()));
                l6.add(new OrderInfo("详细地址", oIn.getCollectGoodsAddress()));
            }
        }

        // 获取支付方式  支付类型0,微信，1支付宝，2银联，3平台余额,4其他
        public String getPayType(String s) {
            return TextUtils.equals("0", s) ? "微信" : (TextUtils.equals("1", s) ? "支付宝" : (TextUtils.equals("2", s) ? "银联" :
                    (TextUtils.equals("3", s) ? "平台余额" : (TextUtils.equals("4", s) ? "其他" : "未支付"))));
        }

        // 获取预订单状态  支付状态0：待支付，1：已支付,2:已支付定金 3已取消,4:已通知用户支付尾款  5：删除
        public String getReserveStatus(String s) {
            if (TextUtils.equals("0", s)) {
                return "待支付订金";
            } else if (TextUtils.equals("1", s)) {
                return "支付完成";
            } else if (TextUtils.equals("2", s)) {
                return "待通知支付尾款";
            } else if (TextUtils.equals("3", s)) {
                return "已取消";
            } else if (TextUtils.equals("4", s)) {
                return "待支付尾款";
            } else if (TextUtils.equals("5", s)) {
                return "已删除";
            }
            return "未知状态";
        }

        // 获取拼团状态 0：已超时、1：进行中、2：已完成
        public String getCollageStatus(String s) {
            return TextUtils.equals("0", s) ? "已超时" : (TextUtils.equals("1", s) ? "进行中"
                    : (TextUtils.equals("2", s) ? "已完成" : "未知状态"));
        }
    }
}
